package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.AddressBookDTO;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import com.sky.vo.AddressBookVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description C端地址簿业务接口实现类
 * @date 2024/08/25 16:41:12
 */
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     * @param addressBookDTO 新增地址数据传输数据模型
     * @return 新增操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    public boolean addAddress(AddressBookDTO addressBookDTO) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDTO, addressBook);

        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);

        //默认不是默认地址
        addressBook.setIsDefault(0);
        int insert = addressBookMapper.insertSelective(addressBook);
        return insert == 1;
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return 地址簿列表
     */
    @Override
    public List<AddressBookVO> getAddressList() {
        List<AddressBookVO> addressBookVOList = new ArrayList<>();
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBookList = addressBookMapper.selectByUserId(userId);
        log.info("查询到的当前用户的地址信息:{}", addressBookList);

        //遍历地址簿列表，将地址簿信息封装到地址簿视图对象中
        if (addressBookList != null && !addressBookList.isEmpty()) {
            for (AddressBook addressBook : addressBookList) {
                AddressBookVO addressBookVO = new AddressBookVO();
                BeanUtils.copyProperties(addressBook, addressBookVO);
                addressBookVOList.add(addressBookVO);
            }
        }
        return addressBookVOList;
    }

    /**
     * 查询当前登录用户的默认地址信息
     * @return 地址信息结果对象
     */
    @Override
    public AddressBookVO getDefaultAddress() {
        AddressBookVO addressBookVO = new AddressBookVO();
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        AddressBook addressBook = addressBookMapper.selectByUserIdAndIsDefault(userId);

        log.info("查询到的当前用户的默认地址信息:{}", addressBook);
        if (addressBook != null) {
            BeanUtils.copyProperties(addressBook, addressBookVO);
        }

        return addressBookVO;
    }

    /**
     * 根据id查询地址
     * @param id 地址id
     * @return 地址信息结果对象
     */
    @Override
    public AddressBookVO getAddressById(Long id) {
        AddressBookVO addressBookVO = new AddressBookVO();
        AddressBook addressBook = addressBookMapper.selectByPrimaryKey(id);
        log.info("查询到的地址信息:{}", addressBook);
        if (addressBook != null) {
            BeanUtils.copyProperties(addressBook, addressBookVO);
        }
        return addressBookVO;
    }

    /**
     * 根据id修改地址
     * @param addressBookDTO 修改地址数据
     * @return 修改地址成功或失败信息结果对象
     */
    @Override
    public boolean updateAddress(AddressBookDTO addressBookDTO) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDTO, addressBook);
        int update = addressBookMapper.updateByPrimaryKeySelective(addressBook);
        return update == 1;
    }

    /**
     * 根据id删除地址
     * @param id 地址id
     * @return 删除操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    public boolean removeAddress(Long id) {
        int delete = addressBookMapper.deleteByPrimaryKey(id);
        return delete == 1;
    }

    /**
     * 设置默认地址
     * @param addressBookDTO 地址簿数据传输数据模型
     * @return 设置默认地址操作是否成功，返回 true 表示成功，false 表示失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setDefaultAddress(AddressBookDTO addressBookDTO) {
        log.info("AddressBookDTO:{}", addressBookDTO);
        //先把此账户下的地址都设置为非默认地址
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        int updateByUserIdAndIsDefault = addressBookMapper.updateByUserIdAndNonDefault(userId);

        log.info("将此账户下的地址都设置为非默认地址:影响条数：{}", updateByUserIdAndIsDefault);
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDTO, addressBook);
        //设置为默认地址
        addressBook.setIsDefault(1);
        int update = addressBookMapper.updateByPrimaryKeySelective(addressBook);
        log.info("设置默认地址:影响条数：{}", update);
        return update == 1;
    }


}
